package com.triangle.cashier.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.triangle.cashier.R;
import com.triangle.cashier.bean.SelectedGoodsItem;

import java.util.List;

/**
 * 选择商品列表适配器
 * Created by ShangJR on 2018/6/30.
 */

public class SelectedGoodsAdapter extends BaseAdapter {
    private LayoutInflater mInflater;//得到一个LayoutInfalter对象用来导入布局
    private Context context;
    private List<SelectedGoodsItem> list;
    private OnChangeOnListener onChangeOnListener;

    public SelectedGoodsAdapter(Context context, List<SelectedGoodsItem> list, OnChangeOnListener listener) {
        this.context = context;
        this.list = list;
        this.mInflater = LayoutInflater.from(context);
        this.onChangeOnListener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.select_goods_item, null);
            holder = new ViewHolder();
            holder.titleTextView = convertView.findViewById(R.id.name_textView);
            holder.goodsCountTextView = convertView.findViewById(R.id.goods_count);
            holder.minusSignBtn = convertView.findViewById(R.id.minus_sign_btn);
            holder.plusSignBtn = convertView.findViewById(R.id.plus_sign_btn);
            holder.deleteBtn = convertView.findViewById(R.id.delete_btn);
            holder.unitTextView = convertView.findViewById(R.id.unit_textView);
            holder.numTextView = convertView.findViewById(R.id.num_textView);
            holder.priceTextView = convertView.findViewById(R.id.price_textView);
            holder.totalTextView = convertView.findViewById(R.id.total_textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        SelectedGoodsItem item = list.get(position);
        holder.titleTextView.setText(item.getName());
        holder.numTextView.setText("编号" + item.getNum());
        holder.priceTextView.setText(item.getPrice() + "元");
        holder.totalTextView.setText(item.getTotal() + "元");
        holder.goodsCountTextView.setText(item.getCount() + "");
        if (item.getCount() <= 1) {
            holder.minusSignBtn.setEnabled(false);
        } else {
            holder.minusSignBtn.setEnabled(true);
        }
        if (position % 2 == 1) {
            convertView.setBackgroundColor(context.getColor(R.color.selected_goods_odd_line_bg));
        } else {
            convertView.setBackgroundColor(context.getColor(R.color.selected_goods_even_line_bg));
        }

        holder.minusSignBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onChangeOnListener.reduce(position);

            }
        });
        holder.plusSignBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                onChangeOnListener.add(position);
            }
        });
        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onChangeOnListener.delete(position);
            }
        });
        return convertView;
    }


    private void refresh() {
        this.notifyDataSetChanged();
    }

    public interface OnChangeOnListener {
        void refresh();

        void add(int position);

        void reduce(int position);

        void delete(int position);
    }

    /**
     * 存放控件
     */
    private final class ViewHolder {
        TextView titleTextView;
        ImageButton minusSignBtn;
        ImageButton plusSignBtn;
        TextView goodsCountTextView;
        ImageButton deleteBtn;
        TextView unitTextView;
        TextView numTextView;
        TextView priceTextView;
        TextView totalTextView;

    }


}
